import http from "./http";
import Global from "./Global";
export default {
    //读取平台配置中心参数信息
    async GetConfiguration() {
        let param = {
            DataCount: 0,
            PageCount: 0,
            PageIndex: 1,
            PageSize: 9999,
        };
        let res = null;
        res = await http.POST(`${Global.urlbefore}/api/System/GetCustomSettings`, param);
        let api = {
            api: '',
        };
        if (res.Code == 200) {
            res.Result.Datas.forEach(item => {
                if (item.SettingName == 'ApiUrl') { //配置中心的配置项名称
                    api.api = item.SettingValue;
                }
            });
        }
        return api;
    },

    //依据搜索添加获取 第一条详细数
    async GetListDataBySeach(
        tableid,
        seachjson,
        SortName,
        SortType,
        PageIndex = 1,
        PageSize = 2000
    ) {
        var dataInfo = [];
        var data = await http.GET(
            Global.urlbefore + "/api/app/GetAppItemView/" + tableid,
            "application/x-www-form-urlencoded"
        );
        var DataUrl =
            Global.urlbefore + "/" + data.Result.Container.Components[0].DataUrl;
        var parames = {
            AppItemCode: tableid,
            PagingInfo: {
                $type: "S2Cloud.Paging.PagingInfo, S2Cloud.SmartFlow",
                DataCount: 0,
                PageCount: 0,
                PageIndex: PageIndex,
                PageSize: PageSize,
            },
            SearchCondition: seachjson,
            SortName: "CreatedTime",
            SortType: "desc",
        };
        data = await http.POST(DataUrl, parames, "application/json");
        dataInfo = data.Result.Datas;
        return dataInfo;
    },

    async GetListDataWithOutCondition(
        tableid,
        seachjson,
        SortName,
        SortType,
        PageIndex = 1,
        PageSize = 2000
    ) {
        var dataInfo = [];
        var data = await http.GET(
            Global.urlbefore + "/api/app/GetAppItemView/" + tableid,
            "application/x-www-form-urlencoded"
        );
        var DataUrl =
            Global.urlbefore + "/" + data.Result.Container.Components[0].DataUrl;
        var parames = {
            AppItemCode: tableid,
            PagingInfo: {
                $type: "S2Cloud.Paging.PagingInfo, S2Cloud.SmartFlow",
                DataCount: 0,
                PageCount: 0,
                PageIndex: PageIndex,
                PageSize: PageSize,
            },
            SearchCondition: seachjson,
        };
        data = await http.POST(DataUrl, parames, "application/json");
        dataInfo = data.Result.Datas;
        return dataInfo;
    },

    // 添加维修管理视图模块
    //依据搜索添加获取 第一条详细数
    async GetReportSeach(
        tableid,
        seachjson,
        SortName,
        SortType,
        PageIndex = 1,
        PageSize = 10
    ) {
        var dataInfo = [];
        var data = await http.GET(
            Global.urlbefore + "/api/app/GetAppItemView/" + tableid,
            "application/x-www-form-urlencoded"
        );
        var DataUrl =
            Global.urlbefore + "/" + data.Result.Container.Components[0].DataUrl;
        var parames = {
            AppItemCode: tableid,
            PagingInfo: {
                $type: "S2Cloud.Paging.PagingInfo, S2Cloud.SmartFlow",
                DataCount: 0,
                PageCount: 0,
                PageIndex: PageIndex,
                PageSize: PageSize,
            },
            SearchCondition: seachjson,
            SortName: SortName,
            SortType: SortType,
            SelfSearchCondition: {
                $type: "S2Cloud.List.SelfSearchCondition, S2Cloud.SmartFlow",
                AppItemCode: "7fed2aed1574ae4c",
                ConditionName: "全部",
                DirtyProperties: [
                    "ConditionName",
                    "SystemOwned",
                    "IsShow",
                    "AppItemCode",
                    "UserID",
                    "IsDefault",
                ],
                IsDefault: true,
                IsShow: true,
                IsSystem: false,
                ListScene: 0,
                ObjectID: "904fb52c-6dff-4dd3-b991-d31c33441400",
                ParentIndex: 0,
                ParentObjectID: "",
                ParentPropertyName: null,
                QueryType: "And",
                SearchCondition: null,
                Serialized: false,
                SortKey: 0,
                SystemOwned: true,
                UserID: localStorage.getItem("UserId"),
            },
        };
        data = await http.POST(DataUrl, parames, "application/json");
        dataInfo = data.Result.Datas;
        return dataInfo;
    },
    //依据搜索添加获取 第一条详细数
    // async  GetListsubDataByID(tableid, subtableid, objectid) {

    //     return dataitem;
    // },
    //通过object id 获取详细数据信息
    //tableid 主表forcode
    //DataId 主表obejectid
    //FlowId 工作流ID
    //isworkflow 如果时工作流传true
    async GetDataInfoByID(tableid, DataId, FlowId, isworkflow = false) {
        let type = "";
        if (!isworkflow) {
            type =
                "S2Cloud.SmartFlow.Sheet.NormalMainViewFormScene,S2Cloud.SmartFlow";
        } else {
            type =
                "S2Cloud.SmartFlow.Sheet.WorkFlowMainUnFinishFormScene,S2Cloud.SmartFlow";
        }
        var jsondata = [];
        var objectid = DataId;
        var parames = {
            $type: type,
            CardType: 0,
            DataId: objectid,
            FormCode: tableid,
            IsMobile: true,
            FlowId: FlowId,
        };
        var data = await http.POST(
            Global.urlbefore + "/api/Form/GetFormInfo/",
            parames,
            "application/json"
        );
        jsondata = data.Result.WebLayout.layoutDetail;
        let dataitem = {};
        dataitem["WorkItemId"] = data.Result.WorkItemId;
        for (const element of jsondata) {
            if (element.componentKey == "ImageComponent") {
                dataitem[element.code] = element.fileList;
            } else {
                dataitem[element.code] = element.value;
            }
            let title = "";
            if (element.componentKey == "SubTable") {
                let subcode = element.code;
                title = element.title;
                let subdatalist = await this.GetsubdataList(tableid, DataId, subcode);
                dataitem[title] = subdatalist;
            }
        }
        return dataitem;
    },
    //获取子表基本信息
    //tableid 主表formcode
    //childname 子表名称 中文
    //maindataid 主表objectid
    //childDataId 子表objectid
    async GetchilddataByID(tableid, childname, maindataid, childDataId) {
        let dataitem = await this.GetDataInfoByID(tableid, maindataid);
        let childlist = dataitem[childname];
        let childitem = childlist.filter(function(e) {
            return e.objectid == childDataId;
        })[0];
        return childitem;
    },

    async GetdataTemp(tableid) {
        var parames = {
            $type: "S2Cloud.SmartFlow.Sheet.NormalMainViewFormScene,S2Cloud.SmartFlow",
            CardType: 0,
            DataId: "de5008d7-c50c-46cd-b667-dc0b7a4474b5",
            FormCode: tableid,
            IsMobile: true,
        };
        var data = await http.POST(
            Global.urlbefore + "/api/Form/GetFormInfo/",
            parames,
            "application/json"
        );
        let jsondata = data.Result.WebLayout.layoutDetail;
        jsondata.WorkflowVersion = data.Result.WorkflowVersion;
        jsondata.WorkItemId = data.Result.WorkItemId;
        return jsondata;
    },
    async GetWorkFlowid(tableid, dataid) {
        var flowid = "";
        var data = await http.GET(
            Global.urlbefore + "/api/app/GetAppItemView/" + tableid,
            "application/x-www-form-urlencoded"
        );
        var DataUrl =
            Global.urlbefore + "/" + data.Result.Container.Components[0].DataUrl;
        var parames = {
            AppItemCode: tableid,
            PagingInfo: {
                $type: "S2Cloud.Paging.PagingInfo, S2Cloud.SmartFlow",
                DataCount: 0,
                PageCount: 0,
                PageIndex: 1,
                PageSize: 10,
            },
            SearchCondition: [],
        };
        data = await http.POST(DataUrl, parames, "application/json");
        let flowdatalist = data.Result.Datas;
        console.log(flowdatalist);
        flowid = flowdatalist.filter(function(e) {
            return e.ObjectID == dataid;
        }).RunningInstanceId;
        return flowid;
    },
    async GetdataworkflowTemp(tableid, dataid) {
        let flowid = await this.GetWorkFlowid(tableid, dataid);
        var parames = {
            $type: "S2Cloud.SmartFlow.Sheet.WorkFlowMainUnFinishViewFormScene,S2Cloud.SmartFlow",
            CardType: 0,
            DataId: dataid,
            FormCode: tableid,
            FlowId: flowid,
            IsMobile: true,
        };
        var data = await http.POST(
            Global.urlbefore + "/api/Form/GetFormInfo/",
            parames,
            "application/json"
        );
        let jsondata = data.Result.WebLayout.layoutDetail;
        jsondata.WorkflowVersion = data.Result.WorkflowVersion;
        jsondata.WorkItemId = data.Result.WorkItemId;
        jsondata.FlowId = flowid;
        return jsondata;
    },
    async GetdataChildCode(tableid, childtablename) {
        var parames = {
            $type: "S2Cloud.SmartFlow.Sheet.NormalMainViewFormScene,S2Cloud.SmartFlow",
            CardType: 0,
            DataId: "de5008d7-c50c-46cd-b667-dc0b7a4474b5",
            FormCode: tableid,
            IsMobile: true,
        };
        var data = await http.POST(
            Global.urlbefore + "/api/Form/GetFormInfo/",
            parames,
            "application/json"
        );
        let jsondata = data.Result.WebLayout.layoutDetail;
        let childformcode = "";
        let childlist = jsondata.filter(function(e) {
            return e.componentKey == "SubTable" && e.title == childtablename;
        });
        if (childlist.length > 0) {
            childformcode = childlist[0].code;
        }
        return childformcode;
    },
    async GetchilddataTemp(tableid, childid) {
        var parames = {
            $type: "S2Cloud.SmartFlow.Sheet.NormalSubViewFormScene,S2Cloud.SmartFlow",
            DataId: "de5008d7-c50c-46cd-b667-dc0b7a4474b5",
            FormCode: tableid,
            SubCode: childid,
        };
        var data = await http.POST(
            Global.urlbefore + "/api/Form/GetFormInfo/",
            parames,
            "application/json"
        );
        let jsondata = data.Result.WebLayout.layoutDetail;
        return jsondata;
    },
    async GetKeyType(dataform, key) {
        if (key == undefined) {
            alert("请核对传递的key的大小写");
        }
        let mykey = key.substr(0, 12);
        let keytype = dataform.filter(function(e) {
            return e.code == mykey;
        });
        if (keytype.length > 0) {
            return keytype[0].componentKey;
        } else {
            return "";
        }
    },

    async GetsubdataList(tableid, objectid, subcode) {
        let parames = {
            DataId: objectid,
            FormCode: tableid,
            SubCode: subcode,
        };
        let data = await http.POST(
            Global.urlbefore + "/api/Form/GetSubList",
            parames,
            "application/json"
        );
        let dataorg = data.Result.Data.layoutData;
        let datalist = [];
        dataorg.forEach((element) => {
            var datalitem = {};
            datalitem["objectid"] = element.DataId;
            element.Data.forEach((element1) => {
                datalitem[element1.code] = element1.value;
            });
            datalist.push(datalitem);
        });
        return datalist;
    },

    async SaveFormData(tableid, Subtableid, dataid, savedata) {
        let dataform = await this.GetdataTemp(tableid);
        for (const element of savedata) {
            element.ComponentKey = await this.GetKeyType(dataform, element.Key);
        }
        var parames = {
            $type: "S2Cloud.SmartFlow.Sheet.SaveDataButton,S2Cloud.SmartFlow",
            CardType: 0,
            DataId: dataid,
            FormCode: tableid,
            FormData: savedata,
            IsOriginate: false,
            SubCode: Subtableid,
            SubData: [],
            UserId: localStorage.getItem("UserId"), //LoginService.LoginService.userInfo.ObjectID
        };
        let returndata = await http.POST(
            Global.urlbefore + "/api/Form/SaveFormInfo",
            parames,
            "application/json"
        );
        if (returndata.Code == "200" && returndata.Message == "") {
            return "";
        } else {
            return returndata.Message;
        }
    },
    async LCSave(tableid, Subtableid, dataid, savedata, workitemid) {
        let dataform = await this.GetdataTemp(tableid);
        for (const element of savedata) {
            element.ComponentKey = await this.GetKeyType(dataform, element.Key);
        }
        var parames = {
            $type: "S2Cloud.SmartFlow.Sheet.SubmitDataButton,S2Cloud.SmartFlow",
            CardType: 0,
            DataId: dataid,
            FormCode: tableid,
            FormData: savedata,
            IsOriginate: false,
            SubCode: Subtableid,
            SubData: [],
            UserId: localStorage.getItem("UserId"), //LoginService.LoginService.userInfo.ObjectID,
            WorkItemId: workitemid,
            WorkflowVersion: 17,
        };
        await http.POST(
            Global.urlbefore + "/api/Form/SaveFormInfo",
            parames,
            "application/json"
        );
    },
    //tableid 主表formcode
    //Subtablename 子表名称
    //dataid 主表objectid
    //childdataid 子表objectid
    //childData json数据
    async SavesubFormData(
        tableid,
        Subtablename,
        dataid,
        childdataid,
        childData = []
    ) {
        let childdataform = [{
            FormCode: tableid,
            FormRowData: [{
                DataId: childdataid,
                FormData: childData,
            }, ],
        }, ];
        let Subtableid = await this.GetdataChildCode(tableid, Subtablename);
        var parames = {
            $type: "S2Cloud.SmartFlow.Sheet.SubSaveDataButton,S2Cloud.SmartFlow",
            CardType: 0,
            DataId: dataid,
            FormCode: tableid,
            FormData: [],
            IsOriginate: false,
            SubCode: Subtableid,
            SubData: childdataform,
            UserId: localStorage.getItem("UserId"), //LoginService.LoginService.userInfo.ObjectID
        };

        let result = await http.POST(
            Global.urlbefore + "/api/Form/SaveFormInfo",
            parames,
            "application/json"
        );
        if (result.Code == "200" && result.Message == "") {
            return "";
        } else {
            return result.Message;
        }
    },
    //只针对已经存在的带工作流的表单提�不针对新�
    //tableid 主表formcode
    //dataid 主表objectid
    //savedata json数据
    async SaveEditWorkFlowData(tableid, dataid, savedata) {
        let dataform = await this.GetdataworkflowTemp(tableid, dataid);
        let WorkflowVersion = dataform.WorkflowVersion;
        let workitemid = dataform.WorkItemId;
        let flowid = dataform.FlowId;
        for (const element of savedata) {
            //element.ComponentKey = await this.GetKeyType(dataform, element.Key);
        }
        var parames = {
            $type: "S2Cloud.SmartFlow.Sheet.SubmitDataButton,S2Cloud.SmartFlow",
            CardType: 0,
            DataId: dataid,
            FormCode: tableid,
            FlowId: flowid,
            FormData: savedata,
            IsOriginate: false,
            SubData: [],
            UserId: localStorage.getItem("UserId"), //LoginService.LoginService.userInfo.ObjectID,
            WorkItemId: workitemid,
            WorkflowVersion: WorkflowVersion,
        };
        await http.POST(
            Global.urlbefore + "/api/Form/SaveFormInfo",
            parames,
            "application/json"
        );
    },
    //获取子表基本信息 单独子表查询
    //tableid 主表formcode
    //childid 子表code
    //childDataId 子表objectid
    async GetchilddataByIDNew(tableid, childid, childdataID) {
        var parames = {
            $type: "S2Cloud.SmartFlow.Sheet.NormalSubViewFormScene,S2Cloud.SmartFlow",
            DataId: childdataID,
            FormCode: tableid,
            SubCode: childid,
        };
        var data = await http.POST(
            Global.urlbefore + "/api/Form/GetFormInfo/",
            parames,
            "application/json"
        );
        let jsondata = data.Result.WebLayout.layoutDetail;
        return jsondata;
    },
    async DeleteFormData(tableid, dataid) {
        var parames = {
            $type: "S2Cloud.SmartFlow.Sheet.DeleteButton, S2Cloud.SmartFlow",
            CardType: 0,
            DataId: dataid,
            FormCode: tableid,
            UserId: localStorage.getItem("UserId"), //LoginService.LoginService.userInfo.ObjectID
        };
        await http.POST(
            Global.urlbefore + "/api/Form/SaveFormInfo",
            parames,
            "application/json"
        );
    }
};